Page({
  data: {
    // 老师信息
    teacherInfo: {
      avatar: "/images/teacher-avatar.png",
      name: "李老师",
      subject: "小学数学",
      title: "高级教师 | 5年教学经验 | 市级优秀教师",
      star: 4.9,
      rating: 4.9,
      studentCount: 128,
      contact: {
        wechat: "LaoShi_Math",
        phone: "138****6789",
        email: "math_teacher@example.com"
      },
      intro: "专注小学数学教学5年，擅长通过趣味教学激发学生兴趣，帮助500+学生提升数学成绩。注重培养学生逻辑思维，善于沟通，耐心负责。",
      tags: ["耐心负责", "趣味教学", "逻辑思维", "成绩提升"]
    },

    // 可预约日期
    availableDates: [
      { week: "今天", day: "6/15" },
      { week: "周五", day: "6/16" },
      { week: "周六", day: "6/17" },
      { week: "周日", day: "6/18" },
      { week: "周一", day: "6/19" }
    ],
    currentDateIndex: 0,

    // 可预约时间
    availableTimes: [
      "09:00-10:00", "10:30-11:30", 
      "14:00-15:00", "15:30-16:30", 
      "19:00-20:00"
    ],
    selectedTime: "",

    // 预约内容
    appointmentContent: ""
  },

  // 选择日期
  selectDate(e) {
    this.setData({
      currentDateIndex: e.currentTarget.dataset.index,
      selectedTime: "" // 切换日期时清空已选时间
    });
  },

  // 选择时间
  selectTime(e) {
    this.setData({
      selectedTime: e.currentTarget.dataset.time
    });
  },

  // 输入预约内容
  inputContent(e) {
    this.setData({
      appointmentContent: e.detail.value
    });
  },

  // 提交预约
  submitAppointment() {
    if (!this.data.selectedTime) return;

    const { teacherInfo, availableDates, currentDateIndex, selectedTime, appointmentContent } = this.data;
    const selectedDate = availableDates[currentDateIndex];

    // 模拟预约提交
    wx.showLoading({ title: "提交中..." });
    setTimeout(() => {
      wx.hideLoading();
      wx.showModal({
        title: "预约成功",
        content: `已成功预约${teacherInfo.name}于${selectedDate.day}（${selectedDate.week}）${selectedTime}的沟通\n\n沟通内容：${appointmentContent || "无"}`,
        showCancel: false,
        confirmText: "确认",
        success: () => {
          wx.navigateBack();
        }
      });
    }, 1000);
  },

  // 复制联系方式
  copyContact(type) {
    let content = "";
    switch (type) {
      case "wechat":
        content = this.data.teacherInfo.contact.wechat;
        break;
      case "email":
        content = this.data.teacherInfo.contact.email;
        break;
    }

    wx.setClipboardData({
      data: content,
      success: () => {
        wx.showToast({
          title: "已复制",
          icon: "success",
          duration: 1500
        });
      }
    });
  },

  // 拨打电话
  makePhoneCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.teacherInfo.contact.phone
    });
  },

  // 直接沟通（打开聊天窗口）
  showContactOptions() {
    wx.showActionSheet({
      itemList: ["发送消息", "拨打电话"],
      success: (res) => {
        if (res.tapIndex === 0) {
          // 模拟跳转到聊天页面
          wx.showToast({
            title: "打开聊天窗口",
            icon: "none"
          });
        } else if (res.tapIndex === 1) {
          this.makePhoneCall();
        }
      }
    });
  }
});